<template>
  <div class="project-ceate">
    <el-page-header class="page-head" @back="$router.back()" title="所有项目">
      <template #content>
        <span style="font-weight: 600;">创建项目</span>
      </template>
    </el-page-header>
    <el-form
    label-position="right"
    label-width="120px"
    style="max-width: 800px">
      <el-form-item label="项目名字">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="总设计师">
        <el-select v-model="form.top_designer.user.id" filterable>
          <el-option v-for="user in users"
          :key="user.id"
          :label="user.name"
          :value="user.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="负责人">
        <el-select v-model="form.leader.id" filterable>
          <el-option v-for="user in users"
          :key="user.id"
          :label="user.name"
          :value="user.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="立项时间">
        <el-date-picker
        v-model="dateValue"
        type="date"
        placeholder="Pick a Date"
        format="YYYY 年 M 月 D 日"
        value-format="YYYY-M-D" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
        @click="onSubmit"
        :disabled="!formSubmitEnable">
          创建
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { post } from '../../../utils/common/api';

export default {
  data() {
    return {
      form: {
        name: '',
        leader: {
          id: undefined
        },
        top_designer: {
          user: {
            id: undefined
          }
        },
        start_time: {
          // year: 2023,
          // month: 9,
          // day: 24
        }
      },

      dateValue: undefined,

      formSubmitEnable: true,

      users: []
    }
  },

  methods: {
    onSubmit() {
      var that = this
      if (that.dateValue != undefined) {
        that.form.start_time = {
          year: parseInt(that.dateValue.split('-')[0]),
          month: parseInt(that.dateValue.split('-')[1]),
          day: parseInt(that.dateValue.split('-')[2])
        }
      }
      // console.log(that.form)
      post('/project/create', that.form).then(function(res) {
        ElNotification({
          title: '创建成功',
          message: `您刚刚创建了项目 ${res.name} ，编号为 ${res.id}`,
          type: 'success',
          position: 'bottom-right',
        })
        that.$router.back()
      })
    }
  },

  mounted() {
    var that = this
    post('/user/all', {}).then(function(res) {
      that.users = res.list
    })
  }
}
</script>

<style scoped>
.page-head {
  margin-bottom: 40px;
}
</style>